﻿package bdz.projects.slab.sections {
	import as3.common.ui.menu.TweenedGridList;
	import bdz.common.model.AppEnvironment;
	import bdz.dms.vo.ContentVO;
	import bdz.projects.slab.renderers.MediaRenderer;
	import bdz.dms.data.DMSCollection;
	import bdz.dms.vo.SectionVO;
	import bdz.projects.slab.core.SlabSection;
	import bdz.projects.slab.data.SlabSiteData;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.utils.*;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import bdz.common.utils.BitmapUtils;
	import flash.text.*;
	import as3.common.ui.menu.events.MenuComponentEvent;
	import fl.transitions.easing.*;
	import bdz.common.utils.TweenLite;
	import bdz.projects.slab.events.SlabSiteEvent;
	import bdz.dms.core.DMSGateway;
	import bdz.dms.events.DMSEvent;
	/**
	* ...
	* @author Default
	*/
	public class SlabPrivateGallery extends SlabSection {
		private var timer:PrivateTimer;
		private var list:TweenedGridList;
		private var arrLeft:SimpleButton;
		private var arrRight:SimpleButton;
		private var listCover:MovieClip;
		private var siteData:SlabSiteData = SlabSiteData.getInstance();
		private var env:AppEnvironment = AppEnvironment.getEnv();
		private var current:SectionVO;
		private var previous:DMSCollection;
		private var upcoming:DMSCollection;
		private var exItems:DMSCollection;
		private var details;
		private var footer:PrivateDetails;
		private var info:PrivateInfo;
		private var currentCollection:DMSCollection;
		private var activationTime:Number;
		private var currentTime:Number;
		private var gateway:DMSGateway = DMSGateway.getInstance();
		private var timeinter:Number;
		private var tdiff:Number;
		private var privateDesc:PrivateDesc;
		private var constructed:Boolean = false;
		private var np;
		function SlabPrivateGallery() {
			super();
		}
		override protected function initializeComponent(evt:Event):void {
			
			
			listCover = new MovieClip();
			listCover.x = 14;
			listCover.y = -20;
			BitmapUtils.createBounds(listCover, 580, 416, 0);
			addChild(listCover);
			siteData.addEventListener(SlabSiteEvent.PRIVATE_GALLERY_INFO_LOADED, onPrivateInfoLoaded);
			addEventListener(Event.REMOVED_FROM_STAGE, cleanUp);
			
			privateDesc = new PrivateDesc();
			privateDesc.description = siteData.getSiteInfo().getProperty("pgallery_desc");
			privateDesc.x = -222;
			privateDesc.y = 310;
			addChild(privateDesc);
			
			setUpData();
			super.initializeComponent(evt);
		}
		private function cleanUp(evt:Event):void {
			clearInterval(timeinter);
		}
		private function onPrivateInfoLoaded(evt:SlabSiteEvent):void {
			
			activationTime = Number(current.getProperty("isactive"));
			gateway.getServerVars("server.date", onServerDateReceived);
		}
		private function onServerDateReceived(evt:DMSEvent):void {
			
			var xm:XML = new XML(evt.data);
			var curDate:Date = new Date();
			curDate.setMonth(Number(xm.month.toString())-1);
			curDate.setDate(Number(xm.date.toString()));
			curDate.setFullYear(Number(xm.year.toString()));
			curDate.setHours(Number(xm.hours.toString()), Number(xm.minutes.toString()), Number(xm.seconds.toString()));
			currentTime = curDate.getTime();
			
			var thour:Number = Number(current.getProperty("duration_hours")) * 3600;
			var tmins:Number = Number(current.getProperty("duration_minutes")) * 60;
			thour *= 1000;
			tmins *= 1000;
			
			activationTime += thour + tmins;
			
			
			tdiff = activationTime - currentTime;
			
			if (tdiff > 0) {
				timer = new PrivateTimer();
				timer.x = listCover.x;
				timer.y = -55;
				addChild(timer);
				
				checkTimeDiff();
				
				filterImageList();
				
			}else {
				//timer.setTimeLabel("00 : 00 : 00");
				
				if(np && !contains(np)){
					np = new NoPrivate();
					np.description = "There are no available 20 square exhibit at the moment.";
					np.y = listCover.y +listCover.height / 2;
					np.x = listCover.x;
					addChild(np);
				}
			}
			
		}
		private function checkTimeDiff():void {
			
			var secs = Math.floor(tdiff/1000)
			var mins = Math.floor(secs/60);
			var hours = Math.floor(mins/60);
			secs %= 60;
			mins %= 60;
			var millidisplay = tdiff % 1000;
			if (millidisplay.toString().length == 2) {
				millidisplay = "0" + millidisplay;
			}
			if (millidisplay.toString().length == 1) {
				millidisplay = "00" + millidisplay;
			}
			if (mins < 10) {
				mins = "0" + mins;
			}
			if(secs < 10){
				secs = "0" + secs;
			}
			timer.setTimeLabel(hours + " : " + mins  + " : " + secs + " : " + millidisplay);
			clearInterval(timeinter);
			timeinter = setInterval(checkTimeDiff, 30);
			tdiff -= 30;
		}
		private function listReady(evt:Event):void {
			
			createFooter();
			createInfo();
			
		}
		private function createInfo():void {
			info = new PrivateInfo();
			info.setListing(upcoming, previous, currentTime);
			info.x = listCover.x;
			info.y = footer.y + footer.height;
			addChild(info);
			setUpArrows();
		}
		private function setUpData():void {
			
			
			if(siteData.getPrivateGalleries().length){
				var dcoll:DMSCollection = siteData.getPrivateGalleries();
				upcoming = new DMSCollection();
				previous = new DMSCollection();
				for each(var svo:SectionVO in dcoll) {
					if (svo.getProperty("isactive") == "") {
						upcoming.push(svo);
						upcoming.push(svo);
					}else {
						previous.push(svo);
					}
				}
				var temp:Array = new Array();
				for (var i:Number = 0; i < previous.length;  i++ ) {
					temp.push( { index:i, value:Number(previous[i].getProperty("isactive")) } );
				}
				temp.sortOn("value", Array.NUMERIC);
				current = previous.splice(temp.pop().index, 1)[0];
				
				if (siteData.getPrivateGalleryInfo(current.section_id)) {
					activationTime = Number(current.getProperty("isactive"));
					gateway.getServerVars("server.date", onServerDateReceived);
				}
			}else {
				if(np && !contains(np)){
					np = new NoPrivate();
					np.description = "There are no available 20 square exhibit at the moment.";
					np.y = listCover.y +listCover.height / 2;
					np.x = listCover.x;
					addChild(np);
				}
			}
		}
		private function filterImageList():void {
			
			exItems = new DMSCollection();
			var temp:DMSCollection = siteData.getPrivateGalleryInfo(current.section_id);
			for each(var cvo:ContentVO in temp) {
				if (cvo.getProperty("status") == "available") {
					exItems.push(cvo);
				}
			}
			createImageList(exItems);
		}
		private function createImageList(source:Array):void {
			
			var tmp:MediaRenderer = new MediaRenderer(new Object(), new TextFormat());
			list = new TweenedGridList("bdz.projects.slab.renderers.MediaRenderer");
			list.addEventListener("listReady", listReady);
			list.xpad = 20;
			list.x =  listCover.x;
			list.y =  listCover.y;
			list.labelField = "section_id";
			list.dimOthers = true;
			list.viewableColumns = 1;
			list.showUpToEdge = true;
			list.maxrows = 1;
			list.scrollDuration = .8;
			list.delay = .01;
			list.edgeDelay = .03;
			list.arrayItems = source;
			list.showUpToEdge = true;
			list.offsetEdges = false;
			list.mask = listCover;
			list.easing = Strong.easeInOut;
			addChild(list);
		}
		private function setUpArrows():void{
			if (arrLeft && contains(arrLeft)) {
				try{
					removeChild(arrLeft);
					removeChild(arrRight);
				}catch (e:Error) {
					
				}
			}
			if(list && list.maxhscroll>0){
				arrLeft = new arrowLeft2();
				arrRight = new arrowRight2();
				arrLeft.x = listCover.x-30;
				arrRight.x = listCover.x+listCover.width+20;
				arrLeft.y = arrRight.y = listCover.y+(listCover.height/2);
				arrLeft.visible = false;
				arrRight.alpha = arrLeft.alpha = 0;
				TweenLite.to(arrRight, .8, {alpha:1});
				TweenLite.to(arrLeft, .8, {alpha:1});
				addChild(arrLeft);
				addChild(arrRight);
				arrRight.addEventListener(MouseEvent.CLICK, scrollRight);
				arrLeft.addEventListener(MouseEvent.CLICK, scrollLeft);
				
			}
		}
		private function scrollRight(evt:MouseEvent):void {
			list.scrollLeft();
			if(list.curhscroll == list.maxhscroll){
				arrRight.visible = false;
			}
			if(list.curhscroll){
				arrLeft.visible = true;
			}
			createFooter();
		}
		private function scrollLeft(evt:MouseEvent):void {
			list.scrollRight();
			if(list.curhscroll < list.maxhscroll){
				arrRight.visible = true;
			}
			if(!list.curhscroll){
				arrLeft.visible = false;
			}
			createFooter()
		}
		private function createFooter():void {
			try {
				removeChild(footer);
			}catch (e:Error) {
				
			}
			footer = new PrivateDetails();			
			footer.artwname = exItems[list.curhscroll].content_label;
			footer.artsname = exItems[list.curhscroll].getProperty("artist");
			footer.artdesc = exItems[list.curhscroll].getProperty("desc");
			footer.x = listCover.x;
			footer.y = listCover.y+listCover.height;
			addChild(footer);
			
		}
		
	}
	
}